import java.awt.Image;
import java.awt.TrayIcon;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Properties;

public class PrefProperties2 {

	/** Pre-initialized instance */
	private static PrefProperties2 INSTANCE = new PrefProperties2();

	// Tell if it's the first time we launch the application
	public static Boolean isFirtTime = false;
	
	// Tell what the working directory is
	public static String workingDirectory = "./WorkingDir/Doctor2/";
	
	// Tell what the login is
	public static String login = "Narboux";
	
	public static int myInputPort = 9002;
	
	public static int inputPortMin = 9000;
	public static int inputPortMax = 9003;
	
	// Tell what the password is
	public static String password = null;
	
	// Tell when was the last save 
	public static Date lastSave = null;
	
	// Activate demo mode 
	public static boolean demoMode = false;

	/** Private constructor */
	private PrefProperties2() {
	}

	/** Access point for the singleton instance */
	public static PrefProperties2 getInstance() {
		return INSTANCE;
	}

	/* Save our preferences to a preference file */
	public static void savePreferences() {
		
		Properties prop = new Properties();
		OutputStream output = null;

		try {
			
			// create file if it does not exist
			output = new FileOutputStream("config.properties");

			if(output != null ) {
				
				// -----------------------------------------------------
				// First time
				if (isFirtTime != null) {
					prop.setProperty("isFirtTime", Boolean.toString(isFirtTime));
				}

				// -----------------------------------------------------
				// Working directory
				if (workingDirectory != null) {
					prop.setProperty("workingDirectory",
							workingDirectory.toString());
				}

				// -----------------------------------------------------
				// password
				if (password != null) {
					prop.setProperty("password", password);
				}
				
				// -----------------------------------------------------
				// Last save
				if (lastSave != null) {
					
					// Create an instance of SimpleDateFormat used for formatting 
					// the string representation of date (month/day/year)
					DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

					// Get the date today using Calendar object.
					Date today = (Date) Calendar.getInstance().getTime();
					
					// Using DateFormat format method we can create a string 
					// representation of a date with the defined format.
					String reportDate = df.format(today);
					
					prop.setProperty("lastSave", reportDate);
				}

				// save properties to project root folder
				prop.store(output, null);
			}
			

		} catch (IOException io) {
			io.printStackTrace();
		} finally {
			if (output != null) {
				try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}
	}
	
	public static void launchRoutine()
	{
		// Create routine instance for sending file
		SendRoutine2 threadSend = new SendRoutine2();
		threadSend.start();
		Server2 threadServer = new Server2();
		threadServer.start();
	}

	/* Get our preferences from a preference file */
	public static void getPreferences() {
		
		Properties prop = new Properties();
		InputStream input = null;

		try {

			File f = new File("config.properties");
			
			if(f.exists() && !f.isDirectory()) {  
				
				input = new FileInputStream("config.properties");

				if( input != null ) {
					
					// Load a properties file
					prop.load(input);
					
					// -----------------------------------------------------
					// First time
					if(prop.getProperty("isFirtTime") != null) {
						
						isFirtTime = Boolean.valueOf(prop.getProperty("isFirtTime"));
					}
					
					// -----------------------------------------------------
					// Working directory
					if (prop.getProperty("workingDirectory") != null) {
						
						workingDirectory = prop.getProperty("workingDirectory");
					}

					// -----------------------------------------------------
					// Password
					if(prop.getProperty("password") != null ){
						
						password = prop.getProperty("password");
					}
				}
			}
			
			// Create an instance of SimpleDateFormat used for formatting 
			// the string representation of date (month/day/year)
			DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
			
			// -----------------------------------------------------
			// Last save
			if( input != null && prop.getProperty("lastSave") != null ){
				
				lastSave = (Date) df.parse(prop.getProperty("lastSave"));
			}
			else {
				
				lastSave = new Date(0);
			}
			
			//lastSave = (Date) Calendar.getInstance().getTime();
			//lastSave = new Date(0);

		} catch (IOException ex) {
			
			ex.printStackTrace();
			
		} catch (ParseException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	/* Tells if we're on a Mac operating system */
	public static boolean isMac() {
		String osName = System.getProperty("os.name").toLowerCase();
		boolean isMacOs = osName.startsWith("mac os x");
		return isMacOs;
	}
}
